package cn.xianshu.service.impl;

import cn.xianshu.constants.BaseConstants;
import cn.xianshu.domain.Login;
import cn.xianshu.mapper.LoginMapper;
import cn.xianshu.service.ILoginService;
import cn.xianshu.service.IMealService;
import cn.xianshu.util.AssertUtil;
import cn.xianshu.util.encrypt.HttpUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * <p>
 * 登录表 服务实现类
 * </p>
 *
 * @author Uncle Xian
 * @since 2021-11-22
 */
@Service
@Slf4j
public class LoginServiceImpl extends ServiceImpl<LoginMapper, Login> implements ILoginService {


    //密码模式获取Token
    @Value("${oauth2.url.token-password}")
    private  String urlTokenPassword;

    @Value("${oauth2.clientId.system-user}")
    private  String  system;
    @Value("${oauth2.clientId.website-user}")
    private String website;
    @Value("${oauth2.client-password.client-secret}")
    private String clientSecret;

    @Autowired
    private IMealService mealService;


    @Override
    public Login saveLoginAndMeal(Login login, Long mealId) {
        // 判断参数
        if (StringUtils.isEmpty(login.getUsername())){
            throw new RuntimeException("用户名为空!");
        }
        if (StringUtils.isEmpty(login.getPassword())){
            throw new RuntimeException("用户名为空!");
        }
        if (mealId == null){
            throw new RuntimeException("套餐mealId为空!");
        }


        // 保存login
        // 设置默认值
        login.setEnabled(true);
        login.setAccountNonExpired(true);
        login.setAccountNonLocked(true);
        login.setCredentialsNonExpired(true);
        baseMapper.insert(login);
        // 保存mealId
        Long loginId = login.getId();
        mealService.saveRelaationWithLogin(mealId, loginId);
        return login;
    }

    /**
     * 登录
     * @param login
     */
    @Override
    public Map<String, String> login(Login login) {
        AssertUtil.isNotEmpty(login.getUsername(), "用户名不可为空!");
        AssertUtil.isNotEmpty(login.getPassword(), "密码不可为空!");

        Wrapper<Login> wrapper = new EntityWrapper<>();
        wrapper.eq("username", login.getUsername());
        Login loginFromDB = super.selectOne(wrapper);
        String clientId = null;
        if (loginFromDB.getType().intValue() == BaseConstants.login.TYPE_SYSTEM){
            // 后台用户
            clientId = system;
        } else {
            // 前台用户
            clientId = website;
        }
        String completeURL = String.format(urlTokenPassword, clientId, clientSecret, login.getUsername(), login.getPassword());
        // log.info("拼接获取Token -> {}", completeURL);
        Map<String, String> map = HttpUtil.sendPost(completeURL);
        return map;
    }
}
